import request from '@/utils/request'

const get = (url, params = {}, loading = false) => request({
  method: 'get',
  url,
  params,
  loading
})
const post = (url, params, data, loading = true) => request({
  method: 'post',
  url,
  params,
  data,
  loading
})

const put = (url, params, data, loading = true) => request({
  method: 'put',
  url,
  params,
  data,
  loading
})

const del = (url, params, data, loading = true) => request({
  method: 'delete',
  url,
  params,
  data,
  loading
})

/**
 * 公共
 */
const baseUrl = '/v1/common/'
export const http_common = {
  upload: (file, type = 'sys') => {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('type', type)
    return post(baseUrl + 'upload', {}, formData)
  },
  enum: (codes = [
    'MallBusinessType', // 业务类型
    'PublishDataStatusType', // 发布状态
    'SubDataStatusType', // 订阅状态
    'AuditDataStatusType', // 审核状态
    'DataScopeType', // 数据范围
    'ModelType', // 资源类型
    'AuditDeptType', // 审核部门类型
    'AuditModelType', // 审核模块类型
    'AuditResultType', // 审核结果
    'AuditStatusType', // 审核配置状态
    'DictionaryType', // 字典类型
    'ServerType'
  ]) => {
    let params = ''
    for (const code of codes) {
      params += `types=${code}&`
    }
    params = params.slice(0, -1)

    return get(baseUrl + 'enum/select?' + params)
  },
  dict: (codes = [
    'service_provider', // 服务厂商
    'resource_attribute_type', // 资源属性分类
    'resource_storage_format', // 资源存储格式
    'resource_tag', // 资源标签
    'relation_system', // 关联系统
    'update_frequency', // 更新频率
    'provincial_platform_matters', // 省平台事项
    'city_platform_matters', // 市平台事项
    'data_range', // 数据范围
    'data_classification', // 数据分级
    'application_scenario', // 应用场景
    'provide_channels', // 提供渠道
    'realm', // 所属领域
    'evaluate_question', // 评价问题
    'api_param_data_type', // 库表字段类型
    'api_param_position',
    'type'

  ]) => {
    let params = ''
    for (const code of codes) {
      params += `codes=${code}&`
    }
    params = params.slice(0, -1)
    return get(baseUrl + 'dictionary/select?' + params)
  }
}
/** *
 * 用户相关
 * @type {string}
 */
export const http_user = {
  page: (params) => get('/v1/auth/user/list', params)
}
export const http_dept = {
  list: (params) => get('/v1/auth/dept/select', params),
  tree: (params) => get('/v1/auth/dept/treeList', params)
}

export const http_role = {
  page: (params) => get('/v1/auth/role/list', params),
  read: (roleId) => get('/v1/auth/role/detail', { id: roleId }),
  relResource: (params, data) => put('/v1/auth/role/allocResource', params, data)
}
export const http_permission = {
  tree: (params) => get('/v1/auth/resource/treeList', params),
  create: (data) => post('/v1/auth/resource/create', { }, data),
  update: (params, data) => put('/v1/auth/resource/update', params, data),
  delete: (id) => del('/v1/auth/resource/delete', {
    id
  }),
  read: (params) => get('/v1/auth/resource/detail', params)
}

/**
 * 服务应用发布管理
 */
export const http_serve_publish_app = {
  page: (params) => get('/v1/mall/application/publish/list', params),
  create: (params, data) => post('/v1/mall/application/publish/create', params, data),
  update: (params, data) => put('/v1/mall/application/publish/update', params, data),
  upOrDown: (params, data) => put('/v1/mall/application/publish/online_switch', params, data),
  recommend: (params, data) => put('/v1/mall/application/publish/recommend', params, data),
  read: (params) => get('/v1/mall/application/publish/detail', params, true)
}
/**
 * 服务工具发布管理
 */
export const http_serve_publish_tool = {
  page: (params) => get('/v1/mall/tool/publish/list', params),
  create: (params, data) => post('/v1/mall/tool/publish/create', params, data),
  update: (params, data) => put('/v1/mall/tool/publish/update', params, data),
  upOrDown: (params, data) => put('/v1/mall/tool/publish/online_switch', params, data),
  recommend: (params, data) => put('/v1/mall/tool/publish/recommend', params, data),
  read: (params) => get('/v1/mall/tool/publish/detail', params, true)
}
/**
 * 服务数据发布管理
 */
export const http_serve_publish_data = {
  page: (params) => get('/v1/mall/server/publish/list', params),
  create_databse: (params, data) => post('/v1/mall/server/publish/create/database', params, data),
  update_databse: (params, data) => put('/v1/mall/server/publish/update/database', params, data),
  create_api: (params, data) => post('/v1/mall/server/publish/create/api', params, data),
  update_api: (params, data) => put('/v1/mall/server/publish/update/api', params, data),
  upOrDown: (params, data) => put('/v1/mall/server/publish/online_switch', params, data),
  read: (params) => get('/v1/mall/server/publish/detail', params, false),
  getDatabaseInfo: (params) => get('/v1/resource/databaseInfo/select', params, false)
}

/**
 * 服务应用订阅
 */
export const http_serve_sub_app = {
  page: (params) => get('/v1/mall/application/sub/list', params),
  read: (params) => get('/v1/mall/application/sub/detail', params, true),
  // 退订
  unsub: (params, data) => del('/v1/mall/sub/un', params, data),
  // 应用成效
  effectDetail: (params) => get('/v1/mall/app_effect/detail', params, true),
  effectCreate: (params, data) => post('/v1/mall/app_effect/create', params, data, true)
}
/**
 * 服务工具订阅
 */
export const http_serve_sub_tool = {
  page: (params) => get('/v1/mall/tool/sub/list', params),
  read: (params) => get('/v1/mall/tool/sub/detail', params, true),
  // 退订
  unsub: (params, data) => del('/v1/mall/sub/un', params, data),
  // 应用成效
  effectDetail: (params) => get('/v1/mall/app_effect/detail', params, true),
  effectCreate: (params, data) => post('/v1/mall/app_effect/create', params, data, true)
}

/**
 * 数据服务订阅
 */
export const http_serve_sub_server = {
  page: (params) => get('/v1/mall/server/sub/list', params),
  read: (params) => get('/v1/mall/server/sub/detail', params, true),
  // 退订
  unsub: (params, data) => del('/v1/mall/sub/un', params, data),
  // 应用成效
  effectDetail: (params) => get('/v1/mall/app_effect/detail', params, true),
  effectCreate: (params, data) => post('/v1/mall/app_effect/create', params, data, true)
}

/**
 * 服务应用订阅、工具订阅评价操作
 */
export const http_serve_sub_app_evaluate = {
  page: (params) => get('/v1/mall/evaluate/list', params),
  read: (params) => get('/v1/mall/evaluate/detail', params, true),
  create: (params, data) => post('/v1/mall/evaluate/create', params, data)
}

/**
 * 服务应用发布审核、订阅审核列表
 */
export const http_serve_sub_app_publish_audit = {
  page: (params) => get('/v1/mall/application/publish/audit/list', params)
}
export const http_serve_sub_app_sub_audit = {
  page: (params) => get('/v1/mall/application/sub/audit/list', params)
}

/**
 * 服务工具发布审核、订阅审核列表
 */
export const http_serve_sub_tool_publish_audit = {
  page: (params) => get('/v1/mall/tool/publish/audit/list', params)
}
export const http_serve_sub_tool_sub_audit = {
  page: (params) => get('/v1/mall/tool/sub/audit/list', params)
}

/**
 * 服务数据发布审核、订阅审核列表
 */
export const http_serve_sub_server_publish_audit = {
  page: (params) => get('/v1/mall/server/publish/audit/list', params)
}
export const http_serve_sub_server_sub_audit = {
  page: (params) => get('/v1/mall/server/sub/audit/list', params)
}

/**
 * 审核请求管理 此处均使用auditRequestId
 */
export const http_serve_audit_process = {
  // 处理流程
  list: (params) => get('/v1/audit/request/process', params),
  pass: (params, data) => put('/v1/audit/request/pass', params, data),
  reject: (params, data) => put('/v1/audit/request/reject', params, data),
  revoke: (params, data) => put('/v1/audit/request/revoke', params, data),
  nodeList: (params) => get('/v1/audit/request/nodes', params)
}

/**
 *  商城相关
 */

export const http_shop = {
  countTotal: (params) => get('/v1/portal/count', params)
}
// 商城服务应用
export const http_shop_app = {
  page: (params) => get('/v1/portal/application/list', params, false),
  read: (params) => get('/v1/portal/application/detail', params, false),
  applicationPage: (params) => get('/v1/portal/evaluate/list', params, false),
  sub: (params, data) => post('/v1/portal/application/sub', params, data, true)
}

// 商城服务工具
export const http_shop_tool = {
  page: (params) => get('/v1/portal/tool/list', params, false),
  read: (params) => get('/v1/portal/tool/detail', params, false),
  applicationPage: (params) => get('/v1/portal/evaluate/list', params, false),
  sub: (params, data) => post('/v1/portal/tool/sub', params, data, true)
}
// 商城数据服务
export const http_shop_server = {
  selectList: (params) => get('/v1/portal/server/query', params, false),
  page: (params) => get('/v1/portal/server/list', params, true),
  read: (params) => get('/v1/portal/server/detail', params, true),
  create_database: (params, data) => post('/v1/portal/server/sub/database', params, data, true),
  update_database: (params, data) => put('/v1/portal/server/sub/database/update', params, data, true),
  create_api: (params, data) => post('/v1/portal/server/sub/api', params, data, true),
  update_api: (params, data) => put('/v1/portal/server/sub/api/update', params, data, true)
}
